import { Injectable, Component } from '@angular/core';
import { LoadingController, ToastController, AlertController } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class BaseUiService {
  public loader: any;
  public toast: any
  constructor(
    public toastCtrl: ToastController,
    public alertController: AlertController,
    public loadingCtrl: LoadingController,
    ) { }

  /**
     * loading加载页面
     * @param {LoadingController} loadingCtrl
     * @param {string} message
     * @returns {Loading}
     * @memberof BaseUI
     */
    async showLoading(message: string,duration: number) {
      this.loader = await this.loadingCtrl.create({
          message: message,
          duration: duration,  // 默认展示的时长
      });
      await this.loader.present();
   }

    async hideLoading() {
        await this.loader.dismiss();
    }

  /**
  * Toast全局提示
  * @param {ToastController} toastCtrl
  * @param {string} message 
  * @returns {toast}
  * @memberof BaseUI
  */
  async showToast( message: string,color: string,position: any,) {
    this.hideToast();
    this.toast = await this.toastCtrl.create({
        message: message,
        color: color,
        duration: 2000,  // 默认展示的时长
        position: position,
    });
    await this.toast.present();
    return this.toast;
  }

  async hideToast() {
    await this.toast.dismiss();
}

  /***加载loading*****
   * 
   */
  async presentLoading(message?,time?) {
    const loading = await this.loadingCtrl.create({
      message:message,
      duration: time || "",  // 默认展示的时长
    });
    await loading.present();
    return loading;
  }

  //alert全局弹框提示
  async presentAlert(header:string,subHeader:string,message: string,confirm:any,cancel:any) {
    const alert = await this.alertController.create({
      header: header,
      subHeader: subHeader,
      message: message,
      buttons: [
        {
          text: '取消',
          role: 'cancel',
          handler: cancel
        }, {
          text: '确定',
          handler:confirm
        }
      ]
    });

    await alert.present();
  }

  //密码校验
  passwordVerify(info:any){
    let regular  = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
    if(regular.test(info)){
      return true
    } return false
  }
  //身份证校验
  idCardVerify(info:any){
    let regular  = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if(regular.test(info)){
      return true
    } return false
  }
  //手机号校验
  phoneVerify(info:any){
    let regular  = /^1[3-9]\d{9}$/;
    if(regular.test(info)){
      return true
    } return false
  }
  //姓名校验
  nameVerify(info:any){
    let regular  = /^[\u2E80-\u9FFF]+$/;
    if(regular.test(info)){
      return true
    } return false
  }


}
